/**
 * 
 */
package org.o14x.alpha.ui.views.folder.columns;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.o14x.alpha.ui.controller.UIController;

/**
 * Helper for TableColumn related tasks. 
 * 
 * @author Olivier DANGREAUX
 */
public abstract class ColumnHelper {
	/**
	 * The UIController.
	 */
	private UIController uiController;
	
	/**
	 * Is the Column enabled;
	 */
	private boolean enabled = true;

	/**
	 * Creates a new TableColumn.
	 * 
	 * @param parent The parent table.
	 * @param listener The sort listener.
	 * 
	 * @return The newly created TableColumn.
	 */
	public TableColumn createTableColumn(Table parent, Listener listener) {
		TableColumn tableColumn = new TableColumn(parent, SWT.NONE);
		tableColumn.addListener(SWT.Selection, listener);
		tableColumn.setWidth(100);
		tableColumn.setText(getTitle());
		tableColumn.setMoveable(false);
		tableColumn.setData(ColumnHelper.class.getName(), this);
		return tableColumn;
	}

	/**
	 * Gets the image associated with the given file.
	 * 
	 * @param file A file 
	 * 
	 * @return The image associated with the given file
	 */
	public abstract Image getColumnImage(File file);

	/**
	 * Gets the text associated with the given file.
	 * 
	 * @param file A file 
	 * 
	 * @return The text associated with the given file
	 */
	public abstract String getColumnText(File file);
	
	/**
	 * Gets the title of the column.
	 * 
	 * @return The title of the column.
	 */
	public abstract String getTitle();

	/**
	 * Compares the given files for sorting.
	 * 
	 * @param file1 The first file to be compared.
	 * @param file2 The second file to be compared.
	 * 
	 * @return The result of the comparison.
	 */
	public abstract int compare(File file1, File file2);

	public static ColumnHelper getColumnHelper(TableColumn tableColumn) {
		ColumnHelper columnHelper = null;

		if (tableColumn != null) {
			columnHelper = (ColumnHelper) tableColumn.getData(ColumnHelper.class.getName());
		}

		return columnHelper;
	}

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}

	/**
	 * Returns the value of enabled.
	 *
	 * @return The value of enabled.
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * Sets the value of enabled.
	 *
	 * @param enabled The value of enabled to set.
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
}
